function getDefaultDistributeMessageTitle(){
	return "项目分发信息";
}
function getDefaultDistributeChangeMessageTitle(){
	return "项目变更信息";
}
function getDefaultDistributeMessage(){
	return "您刚被分发了新的项目，请到项目数据模块查看";
}
function getDefaultDistributeChangeMessage(){
	return "您负责的项目变更了跟进人，请到项目数据模块查看";
}
// ColumnModels
var projectDistributeGrid_cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
			header : '项目ID',
			dataIndex : 'projectId',
			sortable : true,
			hidden : true,
			width : 50
		}, {
			header : '项目编号',
			dataIndex : 'projectNumber',
			width : 110,
			sortable : true,
			locked : false
		}, {
			header : '项目名称',
			dataIndex : 'projectName',
			width : 150
		}, {
			header : '主承办单位',
			dataIndex : 'mainUndertakeUnitName',
			width : 80
		}, {
			header : '保证单位',
			dataIndex : 'guaranteeUnitName',
			width : 80
		}, {
			header : '立项年度',
			dataIndex : 'establishProjectYear',
			width : 80
		}, {
			header : '主管处室',
			dataIndex : 'mainManagingDepartment',
			width : 80
		}, {
			header : '跟进人',
			dataIndex : 'followUpPersonName',
			renderer : stateAction,
			width : 100
		},{
			header : '操作',
			dataIndex : 'projectNumber',
			renderer: followAction,
			width : 150
		}])
function stateAction(followUpPerson){
	if(followUpPerson == 0 || followUpPerson == null|| followUpPerson == ""){
		return "未指定";
	}else{
		return followUpPerson;
	}
}
function followAction(val,p,record){
	var followUpPersonId = record.get("followUpPerson");
	var followUpPersonName = record.get("followUpPersonName");
	var followUpPersonJobnumber = record.get("followUpPersonJobnumber");
	if(followUpPersonId == 0 || followUpPersonId == null){
		assign = "assignFollowUpPerson('"+val+"');return false;";
	    a = ' <a href="#" onclick="'+assign+'">指定跟进人</a>&nbsp;';
	    return a;
	}else{
		return;
//		change = "changeFollowUpPerson('"+val +"','" +followUpPersonId+"','" +followUpPersonName+"','" +followUpPersonJobnumber+"');return false;";
//	    b = ' <a href="#" onclick="'+change+'">变更跟进人</a>&nbsp;';
//	    return b;
	}
}


Ext.projectDistributeGrid = Ext.extend(Ext.grid.GridPanel,{
	initialized:false,
	loadMask : true,
	loadData:function(){
	this.store.load({
		params:{
		start:0,
		limit:20
	}
	});
},
	initComponent:function(){
		this.store = projectDistributeStore;
		this.stripeRows = true; 
		this.cm = projectDistributeGrid_cm;
		this.tbar = [new Ext.inquryProjectCombobox({
			id: 'pm_inquryProjectCombobox_id',
			width : 100,
			listeners : {
				"select" : function(combo, record, number) {
					var a = combo.findParentBy(function(ct,cmp){
						return (ct instanceof Ext.grid.GridPanel) ? true : false;
					});
					a.getTopToolbar().findById("pm_inquryProjectTextfield_id").setValue();
			
		        }
								
			}
		}),new Ext.inquryProjectTextfield({
			id : 'pm_inquryProjectTextfield_id'
		}),new Ext.inquryProjectButton({
			id:'pm_inquryProjectButton_id',
			handler : function(b,e){
			var a = b.findParentBy(function(ct, cmp) { 
				return (ct instanceof Ext.grid.GridPanel) ? true : false; 
			});
			var combo_value = a.getTopToolbar().findById("pm_inquryProjectCombobox_id").getValue();
			var text_value = a.getTopToolbar().findById("pm_inquryProjectTextfield_id").getValue();
			if(combo_value == "" && text_value == ""){
//				Ext.Msg.alert("请选择查询字段并输入查询文本!");
//				return;
			}else if(combo_value == ""){
				Ext.Msg.alert("请选择查询字段!");
				return;
			}else if(text_value == ""){
				Ext.Msg.alert("请输入查询文本!");
				return;
			}
				projectDistributeStore.on('beforeload',function(){
					projectDistributeStore.baseParams = {
							inquryText : text_value,
							type : combo_value
					}
				});
				projectDistributeStore.load({
					params : {
						start : 0,
						limit : 20,
						inquryText : text_value,
						type : combo_value
				}
				});
				
			
		}
		})];
		this.bbar = new Ext.PagingToolbar({
			pageSize : 20,
			store : this.store,
			displayInfo : true,
			displayMsg : '显示 {0}-{1}条 / 共 {2} 条',
			emptyMsg : "无数据。",
			plugins : [new Ext.ux.PageSizePlugin()],
			items : ['-']
		});
        Ext.projectDistributeGrid.superclass.initComponent.call(this);
              
}
})

//指定跟进人
var pm_projectnumber;
var pm_currentwindow;

function assignFollowUpPerson(val){
	//save the projectnumber
	pm_projectnumber = val;
	var assignWindow = new Ext.pm.ChooseFollowUpPersonWindow({
		title : '指定跟进人'
	});
	pm_currentwindow = assignWindow;
	assignWindow.grid.store.load({
		params : {
		 start : 0,
		 limit : 20
	}
	});
	assignWindow.show();

}
//更改跟进人
function changeFollowUpPerson(val,followuppersonId,followUpPersonName,followUpPersonJobnumber){
	//show window;
	pm_projectnumber = val;
	var changeWindow = new Ext.pm.ChooseFollowUpPersonWindow({
		title : '变更跟进人',
		buttons : [{
     	   xtype:'button',
     	   text : '确定',
     	   handler : function(){
				var hasChose = changeWindow.grid.getSelectionModel().getSelected();
				if(hasChose){
					var chooice = changeWindow.grid.getSelectionModel().getSelections();
					if(followuppersonId == chooice[0].get("generalmanagerid")){
						Ext.Msg.alert("操作失败","所选择的管理员为原跟进人！");
						return;
					}
					var generalManagerId = chooice[0].get("generalmanagerid");
					var generalMangerName = chooice[0].get("generalmanagername");
					var generalMangerJobNumber = chooice[0].get("jobnumber");
					
					Ext.Ajax.request( {
						url : 'projectManage/projectManageAction!assignFollowUpPerson',
						params : {
						    pageMessage1 : pm_projectnumber,
						    pageMessage2 : generalManagerId,
						    pageMessage3 : getLoginId()
						},
						success : function(){
							changeWindow.close();
							Ext.Msg.alert('操作成功', '成功变更跟进人');
							projectDistributeStore.reload();
							//通知原跟进人
							Ext.Ajax.request( {
								url : 'projectManage/projectManageAction!publishAlertEvent',
								params : {
									pageMessage1 : getDefaultDistributeChangeMessageTitle(),
									pageMessage2 : getLoginJobNumber(),
									pageMessage3 : followUpPersonJobnumber,
									pageMessage5 : getDefaultDistributeChangeMessage(),
									pageMessage6 : getLoginName(),
									pageMessage7 : followUpPersonName
								}
								
							});
							//通知新跟进人
							Ext.Ajax.request( {
								url : 'projectManage/projectManageAction!publishAlertEvent',
								params : {
									pageMessage1 : getDefaultDistributeMessageTitle(),
									pageMessage2 : getLoginJobNumber(),
									pageMessage3 : generalMangerJobNumber,
									pageMessage5 : getDefaultDistributeMessage(),
									pageMessage6 : getLoginName(),
									pageMessage7 : generalMangerName
								}
								
							});
					    },
						failure : function(form, action) {
					    	changeWindow.close();
							Ext.Msg.alert('操作失败', '操作失败，请重新操作！');
						}
					});
				}else{
					Ext.Msg.alert('选择跟进人', '请选择一个跟进人！');
				}
        }
        },{
     	   xtype : 'button',
     	   text : '取消',
     	   handler : function(b, e){
        		changeWindow.close();
        }
        }]
	});
//	pm_currentwindow = changeWindow;
	changeWindow.grid.store.load({
		params : {
		 start : 0,
		 limit : 20
	}
	});
	changeWindow.show();
}


//选择跟进人的弹窗
Ext.pm.ChooseFollowUpPersonWindow = Ext.extend(Ext.Window,{
	modal:true,
//	title:'选择跟进人',
	cloasable : true,
	autoHeight : true,
	autoWidth : true,
	layout:'fit',
	buttonAlign: 'center',
	buttons : [{
	        	   xtype:'button',
	        	   text : '确定',
	        	   handler : function(){
					var hasChose = pm_currentwindow.grid.getSelectionModel().getSelected();
					if(hasChose){
						Ext.MessageBox.confirm("操作确认","你确认要将项目分发给该管理员吗？",function(btn){
							if(btn == "yes"){
								var chooice = pm_currentwindow.grid.getSelectionModel().getSelections();
								var generalManagerId = chooice[0].get("generalmanagerid");
								var generalMangerName = chooice[0].get("generalmanagername");
								var generalMangerJobNumber = chooice[0].get("jobnumber");
								
								Ext.Ajax.request( {
									url : 'projectManage/projectManageAction!assignFollowUpPerson',
									params : {
									    pageMessage1 : pm_projectnumber,
									    pageMessage2 : generalManagerId,
									    pageMessage3 : getLoginId()
									},
									success : function(){
										pm_currentwindow.close();
										Ext.Msg.alert('操作成功', '成功指定跟进人');
										projectDistributeStore.reload();
										Ext.Ajax.request( {
											url : 'projectManage/projectManageAction!publishAlertEvent',
											params : {
												pageMessage1 : getDefaultDistributeMessageTitle(),
												pageMessage2 : getLoginJobNumber(),
												pageMessage3 : generalMangerJobNumber,
												pageMessage5 : getDefaultDistributeMessage(),
												pageMessage6 : getLoginName(),
												pageMessage7 : generalMangerName
											}
											
										});
								    },
									failure : function(form, action) {
								    	pm_currentwindow.close();
										Ext.Msg.alert('操作失败', '操作失败，请重新操作！');
									}
								});
							}
						});
						
					}else{
						Ext.Msg.alert('选择跟进人', '请选择一个跟进人!');
					}
	           }
	           },{
	        	   xtype : 'button',
	        	   text : '取消',
	        	   handler : function(b, e){
//	        	   var a = Ext.getCmp('selectdirector');
//	        	   a.close();
	        	   pm_currentwindow.close();
	           }
	           }],
	initComponent : function(){
	var _reader = new Ext.data.JsonReader(
			{
				root : 'list',
				totalProperty : 'totalCount',
				id : 'id',
				successProperty : '@success'
			}, 
			[ {
				name : 'generalmanagerid',
				mapping : 'generalmanagerid'
			}, {
				name : 'generalmanagername',
				mapping : 'generalmanagername'
			}, {
				name : 'jobnumber',
				mapping : 'jobnumber'
			},{
				name : 'sparefield1',
				mapping : 'sparefield1'
			}, {
				name : 'sparefield2',
				mapping : 'sparefield2'
			}, {
				name : 'sparefield3',
				mapping : 'sparefield3'
			}]);
	
	
	var _store =  new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
            url: 'projectManage/projectManageAction!getAllGeneralManager'
        }),
        pageSize : 20,
        reader : _reader
	});
		
		this.sm = new Ext.grid.CheckboxSelectionModel({
			singleSelect: true
		});
		this.grid =  new Ext.grid.GridPanel({
			id:'pm_followupperson_grid',
			store : _store,
			height : 300,
			width : 420,
			listeners:{
		    	render:function(){
		         	var hd_checker = this.getEl().select('div.x-grid3-hd-checker');
		         	if (hd_checker.hasClass('x-grid3-hd-checker')) {   
		         		hd_checker.removeClass('x-grid3-hd-checker'); // 去掉全选框 
		            } 
		   		}
		    },


			
			sm: this.sm,
			cm  :  new Ext.grid.ColumnModel([ new Ext.grid.RowNumberer(),this.sm, 
			{
				header : '姓名',
				dataIndex : 'generalmanagername',
				width : 100
			}, {
				header : '工号',
				dataIndex : 'jobnumber',
				width : 200
			}]),
			bbar :  new Ext.PagingToolbar({
				pageSize : 20,
				store : _store,
				displayInfo : true,
				displayMsg : '显示 {0}-{1}条 / 共 {2} 条',
				emptyMsg : "无数据。",
				plugins : [new Ext.ux.PageSizePlugin()],
				items : ['-']
			})
		});
		this.items = this.grid;

	Ext.pm.ChooseFollowUpPersonWindow.superclass.initComponent.call(this);
}
});





